package com.todo.strategy.online.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.common.enums.PlanRelationSceneEnum;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.online.application.base.apollo.StrategyApollo;
import com.todo.strategy.online.application.base.apollo.entity.HotCityPlatformStrategyModel;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.PlanRelationBO;
import com.todo.strategy.online.application.entity.bo.PlatformStrategyBO;
import com.todo.strategy.online.application.entity.bo.PlatformStrategyDetailBO;
import com.todo.strategy.online.application.frontend.service.PlatformStrategyAggService;
import com.todo.strategy.online.application.remote.UserInfoRemoteService;
import com.todo.strategy.online.application.service.PlanRelationService;
import com.todo.strategy.online.application.service.PlatformStrategyService;
import com.todo.strategy.online.iface.request.BoutiquePlatformStrategyParam;
import com.todo.strategy.online.iface.request.PlatformStrategyDetailParam;
import com.todo.strategy.online.iface.request.QueryPlatformStrategyRelationParam;
import com.todo.strategy.online.iface.response.PlatformStrategyDetailVO;
import com.todo.strategy.online.iface.response.PlatformStrategyRelationDetailVO;
import com.todo.strategy.online.iface.response.StrategyBaseVO;
import com.todo.user.api.response.BaseUserInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (PlatformStrategy)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:47
 */
@Slf4j
@Service("platformStrategyAggService")
public class PlatformStrategyAggServiceImpl implements PlatformStrategyAggService {

    @Resource
    private PlatformStrategyService platformStrategyService;
    @Resource
    private StrategyApollo strategyApollo;
    @Resource
    private UserInfoRemoteService userInfoRemoteService;
    @Resource
    private PlanRelationService planRelationService;

    @Override
    public BizResponse<PlatformStrategyDetailVO> queryPlatformStrategyById(PlatformStrategyDetailParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyDetailBO platformStrategyDetailBO = platformStrategyService.queryPlatformStrategyDetailById(param.getStrategyId());
        return BizResponse.success(StrategyConvert.convertToPlatformStrategyDetailVO(platformStrategyDetailBO));
    }

    @Override
    public BizResponse<List<StrategyBaseVO>> queryBoutiquePlatformStrategyListByCity(BoutiquePlatformStrategyParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        BizResponse<BaseUserInfoResponse> userInfoResponse = userInfoRemoteService.queryBaseUserAccountInfoByUserId(param.get_user().getUserId());
        if (userInfoResponse == null || !userInfoResponse.isSuccess() || userInfoResponse.getData() == null) {
            log.error("PlatformStrategyAggService#queryBoutiquePlatformStrategyListByCity 当前用户不存在 param：【{}】", JSONObject.toJSONString(param));
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "当前用户不存在");
        }
        HotCityPlatformStrategyModel strategyModel = strategyApollo.queryHotCityPlatformStrategyList(param.getCity());
        if (strategyModel == null || CollectionUtils.isEmpty(strategyModel.getStrategyList())) {
            log.info("PlatformStrategyAggService#queryBoutiquePlatformStrategyListByCity 根据城市查询热门景点配置完成，数据为空 ：【{}】", param.getCity());
            return BizResponse.success();
        }
        List<HotCityPlatformStrategyModel.PlatformStrategySimplyConf> strategyList = strategyModel.getStrategyList();
        List<Long> strategyIdList = strategyList.stream().map(HotCityPlatformStrategyModel.PlatformStrategySimplyConf::getStrategyId).collect(Collectors.toList());
        List<PlatformStrategyBO> responseList = platformStrategyService.queryPlatformStrategyInfoByIdList(strategyIdList);
        if (CollectionUtils.isEmpty(responseList)) {
            log.warn("PlatformStrategyAggService#queryBoutiquePlatformStrategyListByCity 根据攻略Id列表查询数据，数据为空 ：【{}】", JSONObject.toJSONString(strategyIdList));
            return BizResponse.success();
        }
        List<StrategyBaseVO> result = new ArrayList<>();
        for (PlatformStrategyBO platformStrategyBO : responseList) {
            if (platformStrategyBO == null) {
                continue;
            }
            if (StrategyStatusEnum.PUBLISHED.equals(platformStrategyBO.getStatus())) {
                result.add(StrategyConvert.convertToStrategyBaseVO(platformStrategyBO, userInfoResponse.getData()));
            }
        }
        return BizResponse.success(result);
    }

    @Override
    public BizResponse<PlatformStrategyRelationDetailVO> queryPlatformStrategyRelation(QueryPlatformStrategyRelationParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyDetailBO platformStrategyDetailBO = platformStrategyService.queryPlatformStrategyDetailById(param.getStrategyId());
        if (platformStrategyDetailBO == null) {
            return BizResponse.operationFailed("当前数据不存在，请刷新后重试");
        }

        List<PlanRelationBO> planRelationBOList = planRelationService.queryPlanRelationByBizId(param.getStrategyId(), PlanRelationSceneEnum.PLATFORM_STRATEGY);
        return BizResponse.success(StrategyConvert.buildPlatformStrategyRelationDetailVO(platformStrategyDetailBO, planRelationBOList));
    }
}
